// 私有属性和方法
// function Person(firstname, lastname) {
//     var name = firstname + ' ' + lastname;
//     this.getFullName = function () {
//         return name;
//     }
// }
// var my = new Person('luo', 'jianming');
// console.log(my.getFullName());

// 共有属性和方法
// function Person(firstname, lastname) {
//     this.firstname = firstname;
//     this.lastname = lastname;
//     this.getFullName = function () {
//         return firstname + " " + lastname;
//     }
// }
// var my = new Person('luo', 'jianming');
// console.log(my.firstname);
// console.log(my.lastname);
// console.log(my.getFullName());

// 类的属性和方法
// Person.username = 'wangwu';
// console.log(Person.username);

// 用对象的方式给页面添加元素
// var obj = {};
// obj.odiv = document.createElement('div');
// obj.addShow = function () {
//     this.odiv.style = 'width:500px;height:200px;background:pink;';
//     document.body.appendChild(this.odiv);
// }
// obj.addShow();




// 函数里有prototype属性
function Person() {
    this.name = '罗建明';
    this.age = 18;
}
Person.prototype.show = function () {
    console.log(`My name is ${this.name}, I am ${this.age} years old.`);
}
Person.prototype.love = 'code';
console.log(Person.prototype);
console.log(Person.constructor);
// 实例化的对象里有__proto__属性
var my = new Person();
console.log(my.prototype);
console.log(my.constructor);
console.log(my);
console.log(my.name);
console.log(my.__proto__);
console.log(my.__proto__ == Person.prototype); //true

console.log('秋天的风它不曾见过桃花hahaha');


// 求圆的面积原型方法
function Circle(r) {
    this.r = r;
}
Circle.prototype.getArea = function () {
    return Math.PI * Math.pow(this.r, 2);
}
var c1 = new Circle(10);
console.log(c1.getArea());

// 数组排序原型方法(冒泡排序)
Array.prototype.paixu = function () {
    for (let i = 0; i < this.length - 1; i++) {
        for (let j = 0; j < this.length - i - 1; j++) {
            if (this[j] > this[j + 1]) {
                var num = this[j];
                this[j] = this[j + 1];
                this[j + 1] = num;
            }
        }
        console.log(this);
    }
}
var arr = [1, 4, 6, 8, 2, 9, 1, 4, 8];
arr.paixu();


// 设计模式之工厂模式
function humanFactory(param) {
    var obj = new Object();
    obj.name = param[0];
    obj.gender = param[1];
    obj.age = param[2];
    obj.method = param[3];
    return obj;
}
function dogFactory(param) {
    var obj = new Object();
    obj.name = param[0];
    obj.gender = param[1];
    obj.age = param[2];
    obj.method = param[3];
    return obj;
}
function SuperFactory(type, param) {
    switch (type) {
        case 'human':
            return humanFactory(param);
            break;
        case 'dog':
            return dogFactory(param);
            break;
        default:
            break;
    }
}

var luo = new SuperFactory('human', ["luo", "男", 18, function () { return 'hello' }]);
console.log(luo);
console.log(luo.method());

console.log("秋天的风它不曾见过桃花");


function MoveForward(box) {
    this.box = box;
    this.index = 0;
    this.move = function () {
        var that = this; //將this指向赋给that保存
        this.timer = setInterval(() => {
            if (that.index > 7) {
                that.index = 0;
            }
            if (that.box.offsetLeft > 800) {
                that.box.style.left = 0;
            }
            console.log(that.box.offsetLeft);
            that.box.style.backgroundPosition = -160 * that.index + 'px 0';
            that.box.style.left = (that.box.offsetLeft + 20) + 'px';
            that.index++;
        }, 100)
    }
}
var box = document.querySelector('.box');
var bear = new MoveForward(box);
// bear.move();